package com.yaozheng.vocationaltraining.widget;

import com.yaozheng.vocationaltraining.R;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;

/**
 * 我的轨迹学习进度条
 * 
 * @author Administrator
 * 
 */
public class MyLocusLearnProgressBar extends View {
	/**
	 * 圆画笔对象
	 */
	private Paint paint;
	/**
	 * 圆环的颜色
	 */
	private int roundColor;
	/**
	 * 圆环进度颜色
	 */
	private int roundProgressColor;
	/**
	 * 圆环进度宽度
	 */
	private float roundProgressWidth;
	/**
	 * 最大进度
	 */
	private int max = 100;

	private Paint textPaint;

	private Paint textPaint2;
	/**
	 * 当前进度
	 */
	private int currProgress;
	/**
	 * 上面文字
	 */
	private String topStr;

	/**
	 * 底部Str
	 */
	private String buttomStr;
	/**
	 * 中间文字
	 */
	private String centerStr;

	public MyLocusLearnProgressBar(Context context, AttributeSet attrs,
			int defStyleAttr) {
		super(context, attrs, defStyleAttr);
		initView();
	}

	public MyLocusLearnProgressBar(Context context, AttributeSet attrs) {
		super(context, attrs);
		initView();
	}

	public MyLocusLearnProgressBar(Context context) {
		super(context);
		initView();
	}

	@Override
	protected synchronized void onDraw(Canvas canvas) {
		super.onDraw(canvas);

		/**
		 * 圆心
		 */
		paint.setColor(Color.WHITE);
		paint.setStyle(Paint.Style.FILL_AND_STROKE);
		paint.setAntiAlias(true);
		paint.setStrokeWidth(roundProgressWidth);
		canvas.drawCircle(getWidth() / 2, getHeight() / 2, (getWidth() / 2)
				- roundProgressWidth / 2, paint);

		/**
		 * 弧形
		 */
		paint.setStrokeWidth(roundProgressWidth);
		paint.setStyle(Paint.Style.FILL_AND_STROKE);
		paint.setColor(roundProgressColor);
		RectF oval = new RectF(roundProgressWidth / 2, roundProgressWidth / 2,
				getWidth() - roundProgressWidth / 2, getHeight()
						- roundProgressWidth / 2);
		paint.setStyle(Paint.Style.STROKE);
		canvas.drawArc(oval, 360 * 75 / max, 360 * currProgress / max, false,
				paint);

		/**
		 * 圆外边框
		 */
		paint.setStyle(Paint.Style.STROKE);
		paint.setColor(roundColor);
		paint.setStrokeWidth(getResources().getDimensionPixelOffset(
				R.dimen.border_heith));
		canvas.drawCircle(getWidth() / 2, getHeight() / 2, (getWidth() / 2)
				- roundProgressWidth, paint);

		/**
		 * 圆内边框
		 */
		paint.setStyle(Paint.Style.STROKE);
		paint.setColor(roundColor);
		canvas.drawCircle(getWidth() / 2, getHeight() / 2, (getWidth() / 2),
				paint);

		/**
		 * 文字
		 */
		Rect rect = new Rect();
		/**
		 * 上面超过字
		 */
		this.textPaint2.getTextBounds(this.topStr, 0, this.topStr.length(),
				rect);
		int x = (getWidth() / 2) - rect.centerX();// 让现实的字体处于中心位置;;
		// int y = (getHeight() / 2) - rect.centerY();// 让显示的字体处于中心位置;;

		canvas.drawText(topStr, x, roundProgressWidth * 3, textPaint2);

		this.textPaint.getTextBounds(this.centerStr, 0,
				this.centerStr.length(), rect);
		x = (getWidth() / 2) - rect.centerX();// 让现实的字体处于中心位置;;
		int y = (getHeight() / 2) - rect.centerY();// 让显示的字体处于中心位置;;

		/**
		 * 中间内容字
		 */
		canvas.drawText(centerStr, x, y, textPaint);

		this.textPaint2.getTextBounds(this.buttomStr, 0,
				this.buttomStr.length(), rect);
		x = (getWidth() / 2) - rect.centerX();// 让现实的字体处于中心位置;;
		/**
		 * 底部字
		 */
		canvas.drawText(buttomStr, x, getHeight() - roundProgressWidth * 2,
				textPaint2);

	}

	/**
	 * 初始化View
	 */
	public void initView() {
		paint = new Paint();

		roundProgressWidth = getResources().getDimensionPixelSize(
				R.dimen.default_margin);

		roundColor = getResources().getColor(
				R.color.my_locus_learn_round_progress_border_color);// Color.parseColor("#989399");
		roundProgressColor = getResources().getColor(
				R.color.my_locus_learn_round_progress_bg_color);// Color.parseColor("#fda206");

		this.textPaint = new Paint();
		this.textPaint.setAntiAlias(true);// 设置抗锯齿;;;;
		this.textPaint.setColor(roundProgressColor);
		float mTextSize = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,
				20, getResources().getDisplayMetrics());
		this.textPaint.setTextSize(mTextSize);

		this.textPaint2 = new Paint();
		this.textPaint2.setAntiAlias(true);// 设置抗锯齿;;;;
		this.textPaint2.setColor(roundColor);
		float mTextSize2 = TypedValue.applyDimension(
				TypedValue.COMPLEX_UNIT_SP, 12, getResources()
						.getDisplayMetrics());
		this.textPaint2.setTextSize(mTextSize2);
		topStr = "超过";
		buttomStr = "的学员";
		centerStr = "60.7%";
		currProgress = 67;
	}
}
